<?php
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_booking.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_user.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_combo.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_coupon.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_ticket.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_seat.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_seat_status.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_booking_line.php";
require_once DOCUMENT_ROOT . DIR_SHARED_API . "api_cinemas.php";
require_once DOCUMENT_ROOT . DIR_SHARED_API . "api_movies.php";
require_once DOCUMENT_ROOT . DIR_SHARED_API . "api_seats.php";
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_sql_convert.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_code_generator.php';


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of api_security
 *
 * @author Thien
 */
class api_booking {
    
    public static function save_booking($booking_id, $user_id, $ticket_id, 
                $ticket_quantity_solve, $coupon_id, $combo_id, $combo_quantity_solve, $paid) {
        $dao_booking = new dao_booking();
        $total_money = self::calculate_total_money($ticket_id, $ticket_quantity_solve,
            $combo_id, $combo_quantity_solve, $coupon_id);
        return $dao_booking->save($booking_id, $user_id, $ticket_id, 
                $ticket_quantity_solve, $coupon_id, $combo_id, $combo_quantity_solve, $total_money, $paid);
    }
    
    public static function finalize_booking(
        $booking_id, 
        $user_id, 
        $ticket_id, 
        $ticket_quantity, 
        $coupon_id, 
        $combo_id, 
        $combo_quantity,
        $paid) {
        
        self::save_booking(
            $booking_id, 
            $user_id, 
            $ticket_id, 
            $ticket_quantity, 
            $coupon_id, 
            $combo_id, 
            $combo_quantity,
            $paid);
        
        $booking_code = self::generate_code($booking_id);

        $lines = self::get_all_booking_lines_by_booking_id(
                $booking_id, $user_id);

        foreach ($lines as $item) {
            api_seats::update_seat_status_to_sold($ticket_id, $item['row_pos'], $item['column_pos']);
        }
        return $booking_code;
    }
    
    public static function get_user_email_from_booking_id($booking_id) {
        $dao_booking = new dao_booking();
        $booking = $dao_booking->get_by_id($booking_id);
        return $booking['user_email'];
    }
    
    public static function calculate_total_money($ticket_id, $ticket_quantity_solve,
            $combo_id, $combo_quantity_solve, $coupon_id) {
        $total_money = 0;
        
        $dao_ticket = new dao_ticket();
        $ticket = $dao_ticket->get_by_id($ticket_id);
        $ticket_price = $ticket['price'];
        $total_ticket_money = $ticket_price * $ticket_quantity_solve;
        
        $dao_combo = new dao_combo();
        $combo = $dao_combo->get_by_id($combo_id);
        $combo_price = $combo['price'];
        $total_combo_money = $combo_price * $combo_quantity_solve;
        
        $discount_amount = 0;
        $dao_coupon = new dao_coupon();
        $coupon = $dao_coupon->get_by_id($coupon_id);
        if ($coupon['discount_type_id'] == 1) { // == Percentage
            $discount_amount = ($total_combo_money * $coupon['discount_number'] / 100);
        } else if ($coupon['discount_type_id'] == 2) { // == Amount
            if ($coupon['discount_number'] <= $total_combo_money) {
                $discount_amount = $coupon['discount_number'];
            } else {
                $discount_amount = $total_combo_money;
            }
        }
        
        $total_money += $total_ticket_money + $total_combo_money - $discount_amount;
        return $total_money;
    }
    
    public static function is_existing_code($booking_code) {
        $dao_booking = new dao_booking();
        $booking = $dao_booking->get_by_code($booking_code);
        if (empty($booking)) {
            return FALSE;
        }
        return TRUE;
    }
    
    public static function search_existing_code($booking_code) {
        $dao_booking = new dao_booking();
        $booking = $dao_booking->get_by_code($booking_code);
        if (empty($booking) || empty($booking['is_paid'])) {
            return FALSE;
        }
        return TRUE;
    }
    
    public static function update_paid($booking_id) {
        $dao_booking = new dao_booking();
        $dao_booking->update_paid($booking_id);
        return TRUE;
    }
    
    public static function get_booking_code($booking_id) {
        $dao_booking = new dao_booking();
        $booking = $dao_booking->get_by_id($booking_id);
        return $booking['generated_code'];
    }
    
    public static function generate_code($booking_id) {
        $dao_booking = new dao_booking();
        $code = lib_code_generator::generate_random_string();
        while (self::is_existing_code($code)) {
            $code = lib_code_generator::generate_random_string();
        }
        $dao_booking->update_generated_code($booking_id, $code);
        return $code;
    }
    
    public static function book_seat($ticket_id, $row, $column, $book_quantity, $booking_id) {
        $dao_seat = new dao_seat();
        $dao_ticket = new dao_ticket();
        $seat = $dao_seat->get_by_position($ticket_id, $row, $column);
        $result = array();
        $result['success'] = TRUE;
        if ($seat['seat_status_id'] == 1) { // your seat
            if (self::is_your_seat_available($booking_id, $row, $column)) {
                self::delete_booking_line($booking_id, $row, $column);
                $change_status_id = 2;
                if ($seat['is_wheelchair']) {
                    $change_status_id = 3;
                }
                $dao_seat->save($seat['id'], $row, $column, $ticket_id, $change_status_id, 0, 0);
                self::increase_available($ticket_id, 1);
                $result['next_seat_info'] = self::get_seat_with_image_url($seat, $change_status_id);
                $result['changed'] = TRUE;
                $book_quantity++;
            } else {
                $result['changed'] = FALSE;
                $result['notify'] = "This seat is sold, please choose an other.";
            }
        } else if ($seat['seat_status_id'] == 2) { // available
            if ($book_quantity > 0) {
                $change_status_id = 1;
                $dao_seat->save($seat['id'], $row, $column, $ticket_id, $change_status_id, 0, 0);
                self::decrease_available($ticket_id, 1);
                $result['next_seat_info'] = self::get_seat_with_image_url($seat, $change_status_id);
                $result['changed'] = TRUE;
                self::save_booking_line($booking_id, $row, $column);
                $book_quantity--;
            } else {
                $result['changed'] = FALSE;
                $result['notify'] = "Remove your last book to book this, please.";
            }
        } else if ($seat['seat_status_id'] == 3) { // wheelchair
            if ($book_quantity > 0) {
                $change_status_id = 1;
                $dao_seat->save($seat['id'], $row, $column, $ticket_id, $change_status_id, 0, 0);
                self::decrease_available($ticket_id, 1);
                $result['next_seat_info'] = self::get_seat_with_image_url($seat, $change_status_id);
                $result['changed'] = TRUE;
                self::save_booking_line($booking_id, $row, $column);
                $book_quantity--;
            } else {
                $result['changed'] = FALSE;
                $result['notify'] = "Remove your last book to book this, please.";
            }
        } else if ($seat['seat_status_id'] == 4) { // sold
            $result['changed'] = FALSE;
            $result['notify'] = "This seat is sold, please choose an other.";
        } else {
            $result['success'] = FALSE;
        }
        $result['book_quantity'] = $book_quantity;
        return $result;
    }
    
    public static function is_your_seat_available($booking_id, $row, $column) {
        $dao_booking_line = new dao_booking_line();
        $item = $dao_booking_line->get_by_position($booking_id, $row, $column);
//        var_dump($item);
        if (!empty($item)) {
            return TRUE;
        }
        return FALSE;
    }
    
    public static function delete_booking_line($booking_id, $row_pos, $column_pos) {
        $dao_booking_line = new dao_booking_line();
        $item = $dao_booking_line->get_by_position($booking_id, $row_pos, $column_pos);
        if (!empty($item)) {
            $id = $item['id'];
            $dao_booking_line->delete($id);
        }
    }
    
    public static function delete_booking_line_by_booking_id($booking_id) {
        $dao_booking_line = new dao_booking_line();
        $dao_booking_line->delete_by_booking_id($booking_id);
    }
    
    public static function get_all_booking_lines_by_booking_id($booking_id, $user_id) {
        $dao_booking_line = new dao_booking_line();
        $list = $dao_booking_line->get_all_by_booking_id($booking_id, $user_id);
        return $list;
    }
    
    public static function save_booking_line($booking_id, $row_pos, $column_pos) {
        $dao_booking_line = new dao_booking_line();
        $item = $dao_booking_line->get_by_position($booking_id, $row_pos, $column_pos);
        $id = 0;
        if (!empty($item)) {
            $id = $item['id'];
        }
        $dao_booking_line->save($id, $booking_id, $row_pos, $column_pos);
    }
    
    public static function decrease_available($ticket_id, $decrease_number) {
        $dao_ticket = new dao_ticket();
        $ticket = $dao_ticket->get_by_id($ticket_id);
        $available = $ticket['available_seats'];
        $available -= $decrease_number;
        $dao_ticket->update_available($ticket_id, $available);
    }
    
    public static function increase_available($ticket_id, $increase_number) {
        $dao_ticket = new dao_ticket();
        $ticket = $dao_ticket->get_by_id($ticket_id);
        $available = $ticket['available_seats'];
        $available += $increase_number;
        $dao_ticket->update_available($ticket_id, $available);
    }
    
    public static function get_seat_with_image_url($seat, $seat_status_id) {
        $dao_seat_status = new dao_seat_status();
        $seat_status = $dao_seat_status->get_by_id($seat_status_id);
        $seat['image_url'] = LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_TICKET . $seat_status['image'];
        return $seat;
    }
    
    public static function get_all_cities_by_filter($ticket_quantity) {
        $dao_ticket = new dao_ticket();
        $tickets = $dao_ticket->get_all_by_filters($ticket_quantity, 0, 0, 0, "", "");
        $list = array();
        $id_list = array();
        foreach ($tickets as $ticket) {
            $item_id = $ticket['city_id'];
            if (!in_array($item_id,$id_list)) {
                $item = api_cinemas::get_city_by_id($item_id);
                $list[count($list)] = $item;
                $id_list[count($id_list)] = $item_id;
            }
        }
        return $list;
    }
    
    public static function get_all_cinemas_by_filter($ticket_quantity, $city_id) {
        $dao_ticket = new dao_ticket();
        $tickets = $dao_ticket->get_all_by_filters($ticket_quantity, $city_id, 0, 0, "", "");
        $list = array();
        $id_list = array();
        foreach ($tickets as $ticket) {
            $item_id = $ticket['cinema_id'];
            if (!in_array($item_id,$id_list)) {
                $item = api_cinemas::get_cinema_by_id($item_id);
                $list[count($list)] = $item;
                $id_list[count($id_list)] = $item_id;
            }
        }
        return $list;
    }
    
    public static function get_all_movies_by_filter($ticket_quantity, $city_id, $cinema_id) {
        $dao_ticket = new dao_ticket();
        $tickets = $dao_ticket->get_all_by_filters($ticket_quantity, $city_id, $cinema_id, 0, "", "");
        $list = array();
        $id_list = array();
        foreach ($tickets as $ticket) {
            $item_id = $ticket['movie_id'];
            if (!in_array($item_id,$id_list)) {
                $item = api_movies::get_movie_by_id($item_id);
                $list[count($list)] = $item;
                $id_list[count($id_list)] = $item_id;
            }
        }
        return $list;
    }
    
    public static function get_all_show_date_by_filter($ticket_quantity, $city_id, $cinema_id, $movie_id) {
        $dao_ticket = new dao_ticket();
        $tickets = $dao_ticket->get_all_by_filters($ticket_quantity, $city_id, $cinema_id, $movie_id, "", "");
        $list = array();
        $same_list = array();
        foreach ($tickets as $ticket) {
            $item = $ticket['show_date'];
            if (!in_array($item,$same_list)) {
                $list[count($list)] = $item;
                $same_list[count($same_list)] = $item;
            }
        }
        return $list;
    }
    
    public static function get_all_time_start_by_filter($ticket_quantity, $city_id, $cinema_id, $movie_id, $show_date) {
        $dao_ticket = new dao_ticket();
        $tickets = $dao_ticket->get_all_by_filters($ticket_quantity, $city_id, $cinema_id, $movie_id, $show_date, "");
        $list = array();
        $same_list = array();
        foreach ($tickets as $ticket) {
            $item = $ticket['time_start'];
            if (!in_array($item,$same_list)) {
                $list[count($list)] = $item;
                $same_list[count($same_list)] = $item;
            }
        }
        return $list;
    }
    
    public static function is_paid($booking_id) {
        $dao_booking = new dao_booking();
        $booking = $dao_booking->get_by_id($booking_id);
        if (!empty($booking['is_paid']) && $booking['is_paid'] == 1) {
            return TRUE;
        }
        return FALSE;
    }
    
    public static function delete_booking($booking_id) {
        $dao_booking = new dao_booking();
        $booking = self::get_booking_by_id($booking_id);
        $lines = self::get_all_booking_lines_by_booking_id($booking_id, 
                $booking['user_id']);
        foreach ($lines as $item) {
            $seat = api_seats::get_seat($booking['ticket_id'], $item['row_pos'], $item['column_pos']);
            api_seats::unchoose_seat($seat['id']);
        }
        self::delete_booking_line_by_booking_id($booking_id);
        return $dao_booking->delete($booking_id);
    }
    
    public static function get_booking_by_id($booking_id) {
        $dao_booking = new dao_booking();
        return $dao_booking->get_by_id($booking_id);
    }
    
    public static function get_all_booking() {
        $dao_booking = new dao_booking();
        return $dao_booking->get_all();
    }
    
    public static function get_all_booking_paid() {
        $dao_booking = new dao_booking();
        return $dao_booking->get_all_paid();
    }
    
    public static function get_all_bookings_with_now_showing() {
        $dao_booking = new dao_booking();
        $status_id = 2; //for now showing
        return $dao_booking->get_all_by_status_id($status_id);
    }
    
    public static function get_all_bookings_with_comming_soon() {
        $dao_booking = new dao_booking();
        $status_id = 1; //for comming soon
        return $dao_booking->get_all_by_status_id($status_id);
    }
    
    public static function get_all_booking_statuses() {
        $dao_booking_status = new dao_booking_status();
        return $dao_booking_status->get_all();
    }
    
    public static function get_default_user_email() {
        $dao_user = new dao_user();
        $user = $dao_user->get_first();
        if (!empty($user['email'])) {
            return $user['email'];
        }
        return "";
    }
    
    public static function get_default_coupon_code() {
        $dao_coupon = new dao_coupon();
        $coupon = $dao_coupon->get_first();
        if (!empty($coupon['coupon_code'])) {
            return $coupon['coupon_code'];
        }
        return "";
    }
    
    public static function get_default_combo() {
        $dao_combo = new dao_combo();
        $combo = $dao_combo->get_first();
        if (!empty($combo['name'])) {
            return $combo['name'];
        }
        return "";
    }
    
    public static function get_default_ticket() {
        $dao_ticket = new dao_ticket();
        $ticket = $dao_ticket->get_first();
        if (!empty($ticket['name'])) {
            return $ticket['name'];
        }
        return "";
    }
    
    public static function get_ticket_id_from_name($name) {
        $dao_ticket = new dao_ticket();
        $item = $dao_ticket->get_id_by_name($name);
        return $item['id'];
    }
    
    public static function get_user_id_from_email($email) {
        $dao_user = new dao_user();
        $item = $dao_user->get_by_email($email);
        return $item['id'];
    }
    
    public static function get_coupon_id_from_coupon_code($coupon_code) {
        $dao_coupon = new dao_coupon();
        $item = $dao_coupon->get_id_by_coupon_code($coupon_code);
        return $item['id'];
    }
    
    public static function get_combo_id_from_name($name) {
        $dao_combo = new dao_combo();
        $item = $dao_combo->get_id_by_name($name);
        return $item['id'];
    }
    
    public static function validate_booking_fields($email, $ticket, 
                $ticket_quantity_solve, $coupon_code, $combo, $combo_quantity_solve) {
        $error_show = "";
        if (empty($email)) {
            $error_show .= "<li>Please choose Email.</li>";
        }
        if (empty($ticket)) {
            $error_show .= "<li>Please choose Ticket.</li>";
        }
        if (empty($ticket_quantity_solve)) {
            $error_show .= "<li>Please fill Ticket Quantity.</li>";
        }
        if (empty($coupon_code)) {
            $error_show .= "<li>Please choose Coupon Code.</li>";
        }
        if (empty($combo)) {
            $error_show .= "<li>Please choose Combo.</li>";
        }
        if (empty($combo_quantity_solve)) {
            $error_show .= "<li>Please fill Combo Quantity.</li>";
        }
        return $error_show;
    }
    
}
